//
//  CHTextField.swift
//  jyzx
//
//  Created by cyptt on 2022/6/7.
//

import UIKit

extension  UITextField {

  
   public struct chMaxLenght{
       static var max = 0
    }
    /*
     默认占位颜色
     */
   public func setDefaultPlaceholderColor(placeText:String){
        let mutAttString:NSMutableAttributedString = NSMutableAttributedString.init(string: placeText)
        mutAttString.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.init(red: 166/255.0, green: 170/255.0, blue: 178/255.0, alpha: 1),NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15)], range: NSMakeRange(0, mutAttString.length))

        self.attributedPlaceholder =  mutAttString
    }
    
    /*
     最大长度
     */
    public func maxLenght(max:Int){
        chMaxLenght.max = max
        print("最大长度",chMaxLenght.max)
        self.addTarget(self, action: #selector(handleTextFieldTextDidChangeAction), for: UIControl.Event.editingChanged)
    }

  
    @objc func handleTextFieldTextDidChangeAction(){
    
       
        if self.text?.count ?? 0 >= chMaxLenght.max{
            self.text = self.text?.sub(from: 0, to: chMaxLenght.max)
        }
    }
    

}



